<template>
  <div class="shopcar-container">
    <div class="goods-list">
      <div class="mui-content">
        <div class="mui-card" v-for="item in goodslist" :key="item.id">
          <div class="mui-card-content">
            <div class="mui-card-content-inner goods-item">
              <!-- 开关 -->
              <mt-switch
                v-model="selectedObj[item.id]"
                @change="changeSelect(item.id,selectedObj[item.id])"
              ></mt-switch>
              <!-- <mt-button type="danger" size="large"></mt-button> -->
              <!-- 图片 -->
              <img :src="item.thumb_path" alt />

              <!-- 信息区域 -->
              <div class="info">
                <h1>{{ item.title }}--- {{countObj[item.id]}}</h1>
                <div class="goods-info">
                  <span class="price">￥{{ item.sell_price }}</span>

                  <!-- 表示商品对应的数量 -->
                  <nobox :initcount="countObj[item.id]" :id="item.id"></nobox>
                  <a href="#" @click.prevent="del(item.id)">删除</a>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 结算区域 -->
      <div class="mui-card">
        <div class="mui-card-content">
          <div class="mui-card-content-inner jiesuan">
            <div class="left">
              <p>总计（不含运费）</p>
              <p>
                已勾选
                <span class="danger">{{ selectedcount }}</span>商件，总价
                <span class="danger">￥{{ amount }}</span>
              </p>
            </div>
            <mt-button type="danger">去结算 </mt-button>
          </div>
        </div>
      </div>
      {{ JSON.stringify(selectedObj)}}
    </div>
  </div>
</template>

<script>
import nobox from "../sub-components/Shopcar_Nobox.vue";

import { mapGetters, mapMutations } from "vuex";

//  按需导入mint-ui组件
import { Toast } from "mint-ui";

export default {
  data() {
    return {
      goodslist: [] //商品列表
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    ...mapMutations(["delFromCart","changeSelectState"]),
    getGoodsList() {
      if (this.idstr.length <= 0) return;
      this.$http.get("api/goods/getshopcarlist/" + this.idstr).then(result => {
        if (result.body.status === 0) {
          this.goodslist = result.body.message;
        } else {
          Toast("获取商品信息失败！");
        }
      });
    },
    del(id) {
      // 删除商品
      this.goodslist.some((item, i) => {
        if (item.id == id) {
          this.goodslist.splice(i, 1);
          return true;
        }
      });
      // 从vuex 中删除数据
      this.delFromCart(id);
    },
    changeSelect(id, select) {
      // 更改开关的状态
      console.log(id + "----" + select);
      this.changeSelectState({id:id,selected:select})
    },
    
  },
  components: {
    nobox
  },
  computed: {
    ...mapGetters(["idstr", "countObj", "selectedObj","selectedcount","amount"])
  }
};
</script>

<style lang="scss" scoped>
.shopcar-container {
  background-color: #eee;
  overflow: hidden;

  .goods-item {
    display: flex;
    img {
      width: 60px;
      height: 60px;
    }
    h1 {
      font-size: 13px;
    }
    .info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .goods-info {
        align-items: center;
        display: flex;
        justify-content: space-between;
        .price {
          color: red;
          font-weight: bold;
          font-size: 16px;
        }
      }
    }
  }
}

.danger {
  color: red;
  font-weight: bold;
  font-size: 16px;
}

.jiesuan {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
